package club.ruanx.movie.init;

import club.ruanx.acl.entity.SysParam;
import club.ruanx.acl.repository.SysParamRepository;
import club.ruanx.core.init.AbstractSystemInitializer;
import club.ruanx.util.IpUtils;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static club.ruanx.movie.common.constraint.MovieParamConstraint.*;

/**
 * @author 阮胜
 * @date 2018/10/21 20:24
 */
@Component
public class MovieParamInitializer extends AbstractSystemInitializer {
    private static final String PERSON_PROFESSION_REMARK = "人物职业";
    private static final String MOVIE_TYPE_REMARK = "电影类型";
    private static final String LANGUAGE_REMARK = "电影语言";
    private static final String MOVIE_AREA_REMARK = "电影上映地区";
    private final SysParamRepository sysParamRepository;
    private final List<SysParam> paramList = new ArrayList<>();

    @Value("${movie.init.param:true}")
    private boolean isInit;
    private List<String> movieTypes = Stream.of(
            "动作", "喜剧", "悬疑", "科幻", "爱情", "动画", "惊悚", "冒险", "历史", "战争", "奇幻", "武侠", "灾难", "恐怖",
            "犯罪", "音乐", "歌舞", "纪录片"
    ).collect(Collectors.toList());

    private List<String> languages = Stream.of(
            "中文", "粤语", "英语", "日语", "韩语", "泰语", "法语", "俄语", "意大利语", "印度语"
    ).collect(Collectors.toList());

    private List<String> areas = Stream.of(
            "中国大陆", "香港", "美国", "日本", "韩国", "法国", "印度"
    ).collect(Collectors.toList());
    private List<String> sensitiveWords = Stream.of("脏话").collect(Collectors.toList());
    public MovieParamInitializer(SysParamRepository sysParamRepository) {
        this.sysParamRepository = sysParamRepository;
    }

    @Override
    protected void invoke() throws Exception {
        initMovieType();
        initMovieLanguage();
        initMovieAreas();
        initPersonProfession();
        initCommentSensitiveWords();
        paramList.forEach(sysParam -> {
            sysParam.setSeq(0);
            sysParam.setParentId(0);
            sysParam.setOperateIp(IpUtils.getUserIP());
            sysParam.setOperator("系统预置");
            if (!sysParamRepository.existsByFlagAndValue(sysParam.getFlag(), sysParam.getValue())) {
                sysParamRepository.save(sysParam);
            }
        });
    }

    private void initCommentSensitiveWords() {
        paramList.add(SysParam.builder().flag(MOVIE_COMMENT_SENSITIVE_WORD).value(JSON.toJSONString(sensitiveWords)).name("评论敏感词汇过滤列表").build());
    }

    private void initMovieType() {
        int value = 1;
        for (String movieType : movieTypes) {
            paramList.add(SysParam.builder().flag(MOVIE_TYPE).value(String.valueOf(value++)).name(movieType).remark(MOVIE_TYPE_REMARK).build());
        }
    }

    private void initMovieLanguage() {
        int value = 1;
        for (String language : languages) {
            paramList.add(SysParam.builder().flag(MOVIE_LANGUAGE).value(String.valueOf(value++)).name(language).remark(LANGUAGE_REMARK).build());
        }
    }

    private void initMovieAreas() {
        int value = 1;
        for (String area : areas) {
            paramList.add(SysParam.builder().flag(MOVIE_AREA).value(String.valueOf(value++)).name(area).remark(MOVIE_AREA_REMARK).build());
        }
    }


    private void initPersonProfession() {
        paramList.add(SysParam.builder().flag(PERSON_PROFESSION).value("1").name("普通").remark(PERSON_PROFESSION_REMARK).build());
        paramList.add(SysParam.builder().flag(PERSON_PROFESSION).value("2").name("演员").remark(PERSON_PROFESSION_REMARK).build());
        paramList.add(SysParam.builder().flag(PERSON_PROFESSION).value("3").name("导演").remark(PERSON_PROFESSION_REMARK).build());

    }

    @Override
    public int obtainInitIndex() {
        return Integer.MIN_VALUE;
    }

    @Override
    public boolean preInit() throws Exception {
        return isInit;
    }
}
